﻿using System.Windows;
using AnjLab.SyncIT.Core;
using AnjLab.SyncIT.Core.Api;
using Microsoft.Office.Interop.Outlook;
using Exception=System.Exception;

namespace AnjLab.SyncIT.App
{
    /// <summary>
    /// Interaction logic for EditTracSourceWindow.xaml
    /// </summary>
    public partial class EditSourceWindow
    {
        public Source Source { get; set; }

        private readonly ApplicationClass _application;
        private readonly AppointmentItem _item;

        public EditSourceWindow()
        {
            InitializeComponent();
            _application = new ApplicationClass();
            _item = (AppointmentItem)_application.CreateItem(OlItemType.olAppointmentItem);
            Loaded += EditSourceWindow_Loaded;
        }

        void EditSourceWindow_Loaded(object sender, RoutedEventArgs e)
        {
            DataContext = Source;
            if (Source.HasPassword)
            {
                _passwordLabel.Visibility = System.Windows.Visibility.Visible;
                _passwordBox.Visibility = System.Windows.Visibility.Visible;
            }
            _passwordBox.Password = Source.GetPassword();
            outlookTaskDestinationRadioButton.IsChecked = Source.OutlookDestination == OutlookDestination.TaskList;
            outlookCalendarDestinationRadioButton.IsChecked = !outlookTaskDestinationRadioButton.IsChecked;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Source.OutlookDestination = outlookTaskDestinationRadioButton.IsChecked.Value
                                            ? OutlookDestination.TaskList
                                            : OutlookDestination.Calendar;
            Source.SetPassword(_passwordBox.Password);
            DialogResult = true;
        }

        private void Check_Clicked(object sender, RoutedEventArgs e)
        {
            try
            {
                Source.SetPassword(_passwordBox.Password);

                Source.GetTaskDao().CheckConnection();
                
                MessageBox.Show(
                    "Good connection", 
                    "Check connection", 
                    MessageBoxButton.OK, 
                    MessageBoxImage.Information);
            }
            catch(Exception x)
            {
                Library.Log.Error("Error connecting {0}: {1}", Source.ProjectBase, x);
                
                MessageBox.Show(
                    "Bad connection: " + x.Message,
                    "Check connection", 
                    MessageBoxButton.OK, 
                    MessageBoxImage.Warning);
            }
        }

        private void AddOutlookCategories_Click(object sender, RoutedEventArgs e)
        {
            _item.Categories = Source.OutlookCategory;
            _item.ShowCategoriesDialog();
            Source.OutlookCategory = _item.Categories;
        }

        private void UpdateHint(string hint)
        {
            _hintBlock.Text = hint;
        }

        private void ProjectBase_GotFocus(object sender, RoutedEventArgs e)
        {
            UpdateHint(Source.ProjectBaseHint);
        }

        private void Login_GotFocus(object sender, RoutedEventArgs e)
        {
            UpdateHint(Source.UserNameHint);
        }

        private void Password_GotFocus(object sender, RoutedEventArgs e)
        {
            UpdateHint(Source.PasswordHint);
        }

        private void OutlookPrefix_GotFocus(object sender, RoutedEventArgs e)
        {
            UpdateHint(Source.OutlookPrefixHint);
        }

        private void OutlookCategories_GotFocus(object sender, RoutedEventArgs e)
        {
            UpdateHint(Source.OutlookCategoryHint);
        }

        private static string EditTemplateText(string text, bool multiLine)
        {
            var contentDialog = new EditContentTemplateWindow
                                    {
                                        TemplateText = text, 
                                        MultiLine = multiLine
                                    };
            if (contentDialog.ShowDialog() == true)
                return contentDialog.TemplateText;
            return text;
        }

        private void EditContentButton_Click(object sender, RoutedEventArgs e)
        {
            Source.BodyTemplate = EditTemplateText(Source.BodyTemplate, true);
        }

        private void EditSubjectButton_Click(object sender, RoutedEventArgs e)
        {
            Source.SubjectTemplate = EditTemplateText(Source.SubjectTemplate, true);
        }
    }
}
